package com.imti.mapper;

import com.imti.model.*;
import com.imti.model.dto.PaperTitle;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface PaperMapper {

    /**
     * 获取试卷管理列表
     * @return
     */
    @Select("<script>SELECT * FROM t_paper WHERE 1=1" +
            "<if test=\"paper.title!=null and paper.title!=''\"> AND `title` LIKE CONCAT('%',#{paper.title},'%')</if>" +
            " limit #{page.pageIndex},#{page.pageLimit}</script>")
    @Results(id = "pm",value = {
            @Result(property = "radioCount", column = "radio_count"),
            @Result(property = "radioScore", column = "radio_score"),
            @Result(property = "checkCount", column = "check_count"),
            @Result(property = "checkScore", column = "check_score"),
            @Result(property = "judgeCount", column = "judge_count"),
            @Result(property = "judgeScore", column = "judge_score"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "createTime", column = "create_time")
    })
    List<Paper> findAll(Map map);

    @Select("select id from t_paper where title = #{title}")
    Integer selectPaperId(String title);

    @Insert("insert into t_paper (title,radio_count,radio_score,check_count,check_score,judge_count,judge_score,user_id) " +
            "values(#{title},#{radioCount},#{radioScore},#{checkCount},#{checkScore},#{judgeCount},#{judgeScore},#{userId})")
    int addPaper(Paper paper);

    @Results(id = "repo",value = {
            @Result(property = "userId",column = "user_id"),
            @Result(property = "createTime",column = "create_time")
    })
    @Select("select * from t_repo")
    List<Repo> showRepo();

    @Insert("insert into paper_repo (paper_id,repo_id,user_id) values(#{pid},#{repo},#{userId})")
    int saveRepo(@Param("pid") Integer pid,@Param("repo") Integer repo,@Param("userId")Integer userId);

    @Select("select * from t_paper where id = #{id}")
    @ResultMap("pm")
    Paper selectPaperById(Integer id);

    @Select("SELECT tq.id FROM t_question tq,question_repo qr\n" +
            "WHERE \n" +
            "\tqr.question_id=tq.id AND tq.type=#{type} AND tq.id=#{num} AND qr.repo_id\n" +
            "IN (SELECT tr.id FROM t_repo tr,paper_repo pr\n" +
            "WHERE \n" +
            "pr.paper_id=#{id} AND pr.repo_id=tr.id)")
    Integer findTitleById(@Param("id") Integer id, @Param("num") Integer num, @Param("type") Integer type);

    @Select("select count(*) from t_question")
    int selectAllQuestion();

    @Insert("insert into exam_question (question_id,paper_id) values(#{question_id},#{paper_id})")
    int setTitle(@Param("question_id") Integer question_id,@Param("paper_id") Integer paper_id);

//    @Select("SELECT * from t_question tq,exam_question eq\n" +
//            "where tq.id=eq.question_id and eq.paper_id=#{id}")
    @Select("SELECT tq.id as qId,tq.title as questionTitle,tq.type,tq.answer_id,tp.* from t_question tq,exam_question eq,t_paper tp" +
            " where tq.id=eq.question_id and eq.paper_id=tp.id and tq.type=#{type} and eq.paper_id= #{id}")
    @Results({
            @Result(property = "qId",column = "qId"),
            @Result(property = "answerId",column = "answer_id"),
            @Result(property = "questionTitle",column = "questionTitle"),
            @Result(property = "radioCount", column = "radio_count"),
            @Result(property = "radioScore", column = "radio_score"),
            @Result(property = "checkCount", column = "check_count"),
            @Result(property = "checkScore", column = "check_score"),
            @Result(property = "judgeCount", column = "judge_count"),
            @Result(property = "judgeScore", column = "judge_score"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "answerList",
                    column = "qId",
                    javaType = List.class,
                    many = @Many(select = "com.imti.mapper.PaperMapper.findAllAnswerId")
            )
    })
    List<PaperTitle> selectPaperTitleById(@Param("id") Integer id,@Param("type") Integer type);

    @Select("SELECT * FROM t_answer where question_id=#{id}")
    @Results(id = "answer",value = {
            @Result(property = "answerId",column = "id"),
            @Result(property = "questionId",column = "question_id"),
            @Result(property = "correctFlag",column = "correct_flag")
    })
    public List<Answer> findAllAnswerId(Integer id);

    @Delete("delete from exam_question where paper_id = #{id}")
    void deleteAll(Integer id);

    @Select("<script>SELECT count(*) FROM t_paper WHERE 1=1" +
            "<if test=\"title!=null and title!=''\"> AND `title` LIKE CONCAT('%',#{title},'%')</if></script>")
    int getPaperCount(Paper paper);
//    @Select("select count(*) from t_paper")
//    int getPaperCount(Paper paper);

    @Select("SELECT count(*) FROM `exam_question` where paper_id = #{id}")
    int isHavaPaper(Integer id);

    @Delete("delete from t_paper where id = #{id}")
    int deleteRepoById(Integer id);

    @Select("SELECT id AS answerId,`context`,`correct_flag` AS correctFlag FROM t_answer " +
            "WHERE `question_id`=#{id}")
    List<Answer> selectAnswerByQid(Integer id);
}
